<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="medium" :inline="true">
      <el-form-item prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入用户姓名" clearable />
      </el-form-item>
      <el-form-item prop="deptId">
        <treeselect
          v-model="queryParams.deptId"
          :options="deptOptions"
          :normalizer="normalizer"
          placeholder="请选择归属部门"
          class="treeselect_main"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['plan:studyMap:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="7">
        <div class="inner-select">
          <span>学习计划列表：</span>
          <el-select
            v-model="queryParams.planId"
            placeholder="请选择(可搜索)"
            size="small"
            @change="changePlan"
            :clearable="true"
            filterable
            remote
            :remote-method="searchPlan"
            :loading="searchLoading"
            @blur="onBlur"
          >
            <el-option
              v-for="simplePlan in simplePlanList"
              :key="simplePlan.planId"
              :value="simplePlan.planId+''"
              :label="simplePlan.planName"
            >
              <span style="float: left">{{ simplePlan.planName }}</span>
              <dict-tag
                style="float: right;"
                :options="dict.type.plan_is_open"
                :value="simplePlan.planToStart"
              />
            </el-option>
          </el-select>
        </div>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getPlanList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList">
      <el-table-column label="用户名" align="center" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="姓名" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="部门" align="center" prop="deptName" :show-overflow-tooltip="true" />
      <el-table-column
        label="手机号码"
        align="center"
        prop="phonenumber"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="学习进度" align="center" prop="progress">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="当前学习计划的完成进度" placement="top">
            <el-progress
              :text-inside="true"
              :percentage="parseFloat(progressFormat(scope.row.progress))"
              :color="customColorMethod"
              :stroke-width="20"
            />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-data"
            @click="handleCourseList(scope.row)"
            v-hasPermi="['plan:studyMap:list']"
          >课程进度</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getPlanList"
    />

    <!-- 课程进度 -->
    <el-drawer title="学习计划已绑定课程列表!" :visible.sync="dialog.course" direction="rtl" size="50%">
      <el-table v-loading="loading" :data="courseList">
        <el-table-column
          label="课程标题"
          width="100"
          align="center"
          prop="courseTitle"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="课程封面"
          align="center"
          prop="courseCover"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <el-image
              class="courseImg"
              :src="scope.row.courseCover"
              :preview-src-list="[scope.row.courseCover]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="结课时间" align="center" prop="courseEndDate" width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.courseEndDate, '{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否开课" align="center" prop="courseIsBegin">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.course_is_begin" :value="scope.row.courseIsBegin" />
          </template>
        </el-table-column>
        <el-table-column label="学习进度" align="center" prop="progress">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="当前课程的完成进度" placement="top">
              <el-progress
                :text-inside="true"
                :percentage="parseFloat(progressFormat(scope.row.progress))"
                :color="customColorMethod"
                :stroke-width="20"
              />
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleChapterList(scope.row.courseId)"
              v-hasPermi="['plan:plan:edit']"
            >章节进度</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>

    <el-dialog :title="courseTitle" :visible.sync="dialog.chapter" width="30%" append-to-body>
      <el-table v-loading="loading" :data="chapterList">
        <el-table-column
          label="章节名"
          align="center"
          prop="chapterName"
          :show-overflow-tooltip="true"
        />
        <el-table-column label="学习进度" align="center" prop="progress">
          <template slot-scope="scope">
            <el-progress
              :text-inside="true"
              :percentage="parseFloat(progressFormat(scope.row.progress))"
              :color="customColorMethod"
              :stroke-width="20"
            ></el-progress>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPlanProgressList,
  getCourseProgressList,
  getChapterProgressList
} from "@/api/plan/studyMap";
import { listSimpleDept } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listSimplePlan } from "@/api/plan/plan";
export default {
  name: "UserCourse",
  dicts: ["sys_user_sex", "plan_is_open", "course_is_begin"],
  components: {
    Treeselect
  },
  data() {
    return {
      // 用户id
      userId: 0,
      // 弹框开关
      dialog: {
        course: false,
        chapter: false
      },
      // 学习计划搜索loading
      searchLoading: false,
      // 课程名称
      courseTitle: "",
      // 部门简易信息
      deptOptions: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学习计划列表
      planList: [],
      // 课程列表
      courseList: [],
      // 章节列表
      chapterList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        planId: 0,
        deptId: undefined
      },
      // 简易学习计划列表
      simplePlanList: []
    };
  },
  created() {
    this.getSimplePlan();
    this.getListSimpleDept();
  },
  methods: {
    /** 进度格式化 */
    progressFormat(progress) {
      return progress.toFixed(2);
    },
    /** 获取简易学习计划列表 */
    getSimplePlan() {
      listSimplePlan()
        .then(res => {
          this.simplePlanList = res.data;
          if (res.data.length > 0) {
            this.queryParams.planId = res.data[0].planId;
            this.getPlanList();
          }
        })
        .finally(() => {});
    },
    /** 进度颜色 */
    customColorMethod(percentage) {
      if (percentage < 20.0) {
        return "#F56C6C";
      } else if (percentage < 60.0) {
        return "#E6A23C";
      } else if (percentage < 80.0) {
        return "#409EFF";
      } else {
        return "#67C23A";
      }
    },
    /** 获取简易部门列表 */
    getListSimpleDept() {
      listSimpleDept()
        .then(res => {
          this.deptOptions = this.handleTree(res.data, "deptId");
        })
        .finally(() => {});
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    /** 查询用户课程列表 */
    getPlanList() {
      this.loading = true;
      getPlanProgressList(this.queryParams).then(response => {
        this.planList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getPlanList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 更改学习计划 */
    changePlan() {
      this.getPlanList();
    },
    /** 搜索学习计划 */
    searchPlan(planName) {
      this.searchLoading = true;
      setTimeout(() => {
        listSimplePlan(planName == "" ? undefined : { planName: planName })
          .then(res => {
            this.simplePlanList = res.data;
          })
          .finally(() => {});
        this.searchLoading = false;
      }, 200);
    },
    /** 学习计划搜索下拉框失去焦点 */
    onBlur() {
      if (this.simplePlanList.length <= 0) {
        this.searchLoading = true;
        setTimeout(() => {
          listSimplePlan()
            .then(res => {
              this.simplePlanList = res.data;
            })
            .finally(() => {});
          this.searchLoading = false;
        }, 200);
      }
    },
    /** 课程进度列表 */
    handleCourseList(row) {
      this.dialog.course = true;
      this.userId = row.userId;
      let query = {
        userId: row.userId,
        planId: row.planId
      };
      getCourseProgressList(query)
        .then(res => {
          this.courseList = res.data;
        })
        .finally(() => {});
    },
    handleChapterList(courseId) {
      this.dialog.chapter = true;
      let query = {
        courseId: courseId,
        userId: this.userId
      };
      getChapterProgressList(query)
        .then(res => {
          this.chapterList = res.data;
        })
        .finally(() => {});
    },
     /** 导出按钮操作 */
     handleExport() {
      this.download(
        "plan/studyMap/export",
        {
          ...this.queryParams
        },
        `plan_${new Date().getTime()}.xlsx`
      );
    },
  }
};
</script>

<style scoped>
.courseImg {
  display: block;
  margin: auto;
  width: 120px;
  height: 60px;
}
.inner-select {
  display: flex;
  justify-content: center;
}
.inner-select span {
  font-size: 16px;
  color: #909399;
  line-height: 32px;
}
.treeselect_main {
  width: 200px;
  height: 36px;
}
</style>
